<template>
  <el-dialog title="上传素材" :visible.sync="dialogVisible" width="520px" @close="resetForm" destroy-on-close>
    <el-alert v-if="stuffForm.showUpload" type="info" class="mb-4">
      您可以选择上传视频、ppt、word、pdf、md等文件，默认使用文件名作为素材标题。
    </el-alert>

    <el-form :model="stuffForm" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="上级目录" prop="parentId">
        <stuff-dir-select v-model="stuffForm.parentId" />
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input v-model="stuffForm.title"></el-input>
      </el-form-item>

      <el-form-item v-if="stuffForm.showUpload" label="文件" prop="fileUrl">
        <aliyun-oss-upload drag progress show-file-list accept="*" type="stuff" @success="handleUploadSuccess"
          class="w-full">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或 <em>点击上传</em></div>
        </aliyun-oss-upload>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/common/api';
import AliyunOssUpload from '@/components/admin/AliyunOssUpload';
import StuffDirSelect from '@/components/admin/StuffDirSelect';

export default {
  name: 'FileStuffDialog',
  props: {},

  components: {
    StuffDirSelect,
    AliyunOssUpload
  },

  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogTitle: '上传素材',
      stuffForm: {
        parentId: null,
        title: '',
        fileUrl: '',
        showUpload: true
      },
      rules: {
        parentId: [
          { required: true, message: '请选择上级目录', trigger: 'change' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        fileUrl: [
          { required: true, message: '请上传文件', trigger: 'change' }
        ]
      }
    }
  },

  methods: {
    open(data) {
      this.stuffForm = Object.assign({}, this.stuffForm, data);
      this.dialogVisible = true
    },

    resetForm() {
      this.$refs.formRef?.resetFields()
      this.stuffForm = {
        parentId: null,
        title: '',
        fileUrl: '',
        showUpload: true
      }
    },

    handleUploadSuccess({url, name, file}) {
      const data = {
        fileUrl: url,
        fileSize: file.size,        
      }
      const {type} = file;
      console.log('file', file);
      if (type.startsWith('video')) {
        data.stuffType = 'video';
      } else if (name.includes('pptx') || name.includes('ppt')) {
        data.stuffType = 'ppt';
      } else if (type.includes('msword') || name.includes('doc') || name.includes('docx')) {
        data.stuffType = 'doc';
      } else if (type.includes('pdf') || name.includes('pdf')) {
        data.stuffType = 'pdf';
      } else if (type.startsWith('image')) {
        data.stuffType = 'image';
      } else if (type.includes('md') || name.includes('md')) {
        data.stuffType = 'md';
      } else if (type.startsWith('audio')){
        data.stuffType = 'audio';
      } else {
        data.stuffType = 'other';
      }

      
      if (!this.stuffForm.title) {        
        data.title = name.substring(0, name.lastIndexOf('.'));
      }
      this.stuffForm = Object.assign({}, this.stuffForm, data);
    },

    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return

        try {
          this.loading = true
          const {data} = await api.admin_saveStuff(this.stuffForm)
          this.$message.success('上传成功')
          this.dialogVisible = false
          this.$emit('success', data.data)
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
.w-full {
  width: 100%;
}

.mb-4 {
  margin-bottom: 16px;
}
</style>